import 'dart:io';

import 'package:flutter/material.dart';
import 'package:light_image/channels/AndroidBackTop.dart';
import 'package:light_image/config/const.dart';
import 'package:light_image/pages/tool_page.dart';
import 'package:light_image/pages/works_page.dart';
import 'package:light_image/widget/root_tabbar.dart';
import 'home_page.dart';
import 'my_page.dart';

class RootPage extends StatefulWidget {
  @override
  _RootPageState createState() => _RootPageState();
}

class _RootPageState extends State<RootPage> {
  @override
  void initState() {
    super.initState();
//    checkVersion();
  }

//  /// 检查更新 [check update]
//  checkVersion() async {
//    if (Platform.isIOS) return;
//
//    final packageInfo = await PackageInfo.fromPlatform();
//
//    VersionModel model = await versionViewModel.getData();
//
//    int currentVersion = int.parse(removeDot(packageInfo.version));
//
//    int netVersion = int.parse(removeDot(model.appVersion));
//
//    if (currentVersion >= netVersion) {
//      debugPrint('当前版本是最新版本');
//      return;
//    }
//
//    showDialog(
//      context: context,
//      builder: (ctx2) {
//        return UpdateDialog(
//          version: model.appVersion,
//          updateUrl: model.downloadUrl,
//          updateInfo: model.updateInfo,
//          isForce: model.force,
//        );
//      },
//    );
//  }

  @override
  Widget build(BuildContext context) {
    List<TabBarModel> pages = <TabBarModel>[
      new TabBarModel(
        title: '工具',
        icon: new LoadImage("assets/images/bottom_protect.png", false),
        selectIcon: new LoadImage("assets/images/bottom_protect.png", true),
        page: new ToolPage(),
      ),
      new TabBarModel(
        title: '作品',
        icon: new LoadImage("assets/images/bottom_lore.png", false),
        selectIcon: new LoadImage("assets/images/bottom_lore.png", true),
        page: new WorksPage(),
      ),
      new TabBarModel(
        title: '相册',
        icon: new LoadImage("assets/images/bottom_home.png", false),
        selectIcon: new LoadImage("assets/images/bottom_home.png", true),
        page: new HomePage(),
      ),
      new TabBarModel(
        title: '我的',
        icon: new LoadImage("assets/images/bottom_rumor.png", false),
        selectIcon: new LoadImage("assets/images/bottom_rumor.png", true),
        page: new MyPage(),
      ),

    ];
    return WillPopScope(
        onWillPop: () async {
      AndroidBackTop.backDeskTop();  //设置为返回不退出app
      return false;  //一定要return false
    },
    child:new RootTabBar(pages: pages, currentIndex: 0));
  }
}


class LoadImage extends StatelessWidget {
  final String img;
  final bool isSelect;

  LoadImage(this.img, [this.isSelect = false]);

  @override
  Widget build(BuildContext context) {
    return new Container(
      margin: EdgeInsets.only(bottom: 2.0),
      width: 30.0,
      height: 30.0,
      child: new Image.asset(
        img,
        fit: BoxFit.cover,
        gaplessPlayback: true,
        color: isSelect ? fixedColor : mainTextColor,
      ),
    );
  }
}
